﻿using System;
using System.Collections.Generic;
using WEB.Core.Controls;
using Ext.Net;
using WEB.Core.Util;
using log4net;
using WEB.BLL.Services;
using WEB.BLL.Entities;
using WEB.BLL;

namespace WEB.APP.Modules.List
{
    public partial class ListProject : BasePage
    {
        public ListProject()
            : base(SCREENS.LISTPROJECT, false)
        {
        }

        protected override void Page_Load(object sender, EventArgs e)
        {
            AddListControlAdd(tbsAdd);
            this.WindowEditor.SaveClick += new EventHandler(WindowEditor_SaveClick);
            hdScreenDetailID.Text = SCREENS.REPORTPROJECTDETAIL;
            base.Page_Load(sender, e);
        }

        protected override void NotPostBack()
        {
            base.NotPostBack();
            if (!string.IsNullOrEmpty(Request["id"]))
            {
                WindowEditor.Show(Convert.ToDecimal(Request["id"]));
            }
        }

        void WindowEditor_SaveClick(object sender, EventArgs e)
        {
            this.storeMain.DataBind();
        }
        protected void storeMain_OnRefreshData(object sender, StoreRefreshDataEventArgs e)
        {
            try
            {
                var ett = new ProjectEntity()
                {
                    Start = Util.Obj2Int(e.Parameters["start"]),
                    Keyword = txtKeyword.Text.Trim()
                };
                ett.EndNum = Util.Obj2Int(e.Parameters["limit"]) + ett.Start;
                if (CurrentUser.RoleId == 2)
                    ett.Username = CurrentUser.UserName;
                IList<ProjectEntity> lst = cbProjectValid.Checked ? ProjectService.GetInstance().SearchHaveStudent(ett) : ProjectService.GetInstance().Find(ett);
                if (null == lst)
                    return;
                storeMain.DataSource = lst;
                storeMain.DataBind();
                e.Total = ett.TotalRow;
            }
            catch (Exception ex)
            {
                Notification(ex);
            }
        }
        public void btnAdd_Click(object sender, DirectEventArgs e)
        {
            WindowEditor.Show();
        }
        [DirectMethod]
        public void Edit(int id)
        {
            WindowEditor.Show(id);
        }
        [DirectMethod]
        public void Delete(int Id)
        {
            try
            {
                if (Id > 0)
                {
                    var ExpertiseProjectE = new ExpertiseProjectEntity() { ProjectId = Convert.ToDecimal(Id) };
                    ExpertiseProjectService.GetInstance().DeleteByProjectId(ExpertiseProjectE);

                    ProjectService.GetInstance().Delete(new ProjectEntity { ProjectId = ExpertiseProjectE.ProjectId });
                    this.storeMain.DataBind();
                }
            }
            catch (Exception ex)
            {
                Notification(ex);
            }
        }
    }
}
